package com.wsx.sky.sentinel.mock;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.sentinel.mock.SentinelMock
 * @Description: TODO
 * @date 2019/02/28
 */
public class SentinelMock {

    private static ExecutorService service = Executors.newFixedThreadPool(50);

    private static AtomicInteger atom = new AtomicInteger(0);
    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) {

        timeWindow();


        ///initRule(10);

        ///runMock();

        ///runWhile();

        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        service.shutdown();
    }

    private static void initRule(int num) {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("mock");
        rule.setCount(num);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("default");
        rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);

        rules.add(rule);

        FlowRuleManager.loadRules(rules);
    }

    private static void runMock() {
        for (int i = 0; i < 1000; i++) {
            service.submit(new Runnable() {
                @Override
                public void run() {
                    Entry entry = null;
                    try {
                        entry = SphU.entry("mock");
                        ///Thread.sleep(1000);
                        System.out.println("hello" + atom.incrementAndGet());
                    } catch (BlockException b) {
                        System.out.println("block");
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (null != entry) {
                            entry.exit();
                        }
                    }
                }
            });
        }
    }

    private static void runWhile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean stop = true;
                while (stop) {
                    Entry entry = null;
                    try {
                        entry = SphU.entry("mock");
                        count.incrementAndGet();
                    } catch (BlockException be) {
                        System.out.println("block");
                    } catch (Exception e) {

                    } finally {
                        if (null != entry) {
                            entry.exit();
                        }
                    }
                    if (count.intValue() >= 1000) {
                        stop = false;
                    }
                }
            }
        }).start();
    }

    private static void timeWindow() {
        int windowLength = 10;

        Map<String, String> map = new HashMap<>(20);

        int num = 0;
        while (true) {
            num ++;

            long time = System.currentTimeMillis();

            long timeId = time/windowLength;

            int index = (int) (timeId % windowLength);

            long start = time - time % windowLength;

            if (!map.containsKey(index + "")) {
                System.out.println("index:" + index + "::timeId:" + timeId + "::start:" + start + "::time:" + time);
                map.put(index + "", "index" + index);
            } else {
                System.out.println("hehe:index:" + index + "::timeId:" + timeId + "::start:" + start + "::time:" + time);
            }


            if (num >= 1000000) {
                break;
            }
        }

    }
}
